require 'jrake'

#include JRake

JRake.register_servlet('/hello', 'com.example.HelloWorldServlet', ['res', 'tmp/main', 'vendor/stringtemplate/lib/stringtemplate.jar', 'vendor/stringtemplate/lib/antlr-2.7.7.jar'], :main_compile)

task :clean do
  JRake.recursive_delete('tmp')
end

task :default => :unit

task :main_compile do
  src_dir = 'src'
  dest_dir = 'tmp/main'
  classpath = ['vendor/stringtemplate/lib/stringtemplate.jar', 'vendor/jetty/lib/servlet-api-2.5-6.1.0rc0.jar', 'tmp/main']

  JRake.javac(src_dir, dest_dir, classpath)
end

task :unit_compile => :main_compile do 
  src_dir = 'test/unit'
  dest_dir = 'tmp/test/unit'
  classpath = ['tmp/main', 'vendor/junit/junit-4.1.jar']
  
  JRake.javac(src_dir, dest_dir, classpath)
end

task :unit => :unit_compile do
  JRake.junit('tmp/test/unit', ['tmp/main'])
end

def pause(name)
  JRake.log("#{name} - begin")
  x = 1
  (1..1000000).each do |i|
    x += i
  end
  JRake.log("#{name} - end")
end

task :common do
  JRake.log("common - begin")
  JRake.log("common - end")
end

task :server => :common do
  pause :server
end

task :client => :common do
  pause :client
end

task :test => [:client, :server] do
  JRake.log("test - begin")
  JRake.log("test - end")
end